package com.archstudy.push.platform.contract.util;

import com.archstudy.push.platform.contract.dto.MsgDTO;
import com.archstudy.push.platform.contract.dto.MsgStreamDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
public class MsgUtils {


    public static final String SPLIT = "-@-";

    public static String getSessionId(MsgStreamDTO dto) {

        String ref = dto.getAccount() + SPLIT + dto.getAppKey();


        return ref;
    }


    public static List<String> getReceiverIds(MsgStreamDTO dto) {
        if (StringUtils.isBlank(dto.getAppKey())) {
            log.info("pushKey is null");
            return null;
        }
        String ref = dto.getAccount() + SPLIT + dto.getAppKey();


        return Collections.singletonList(ref);
    }

    public static List<String> getReceiverIds(MsgDTO dto) {
        if (StringUtils.isBlank(dto.getAppKey())) {
            log.info("pushKey is null");
            return null;
        }

        String[] targets = dto.getTargetIds().split(",");
        List<String> list=new ArrayList<>(targets.length);

        for (int i = 0; i < targets.length; i++) {
            list.add( targets[i] + SPLIT + dto.getAppKey());
        }


        return list;
    }

    public static String getSessionId(String username, String appKey) {
        return username+SPLIT+appKey;
    }
}
